package com.snowy.hidden.biz.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.snowy.framework.common.base.vo.LoginUser;
import com.snowy.framework.common.resp.BaseResp;
import com.snowy.framework.common.resp.BaseRespUtil;
import com.snowy.framework.exception.UABPException;
import com.snowy.hidden.biz.domain.model.*;
import com.snowy.hidden.biz.domain.req.*;
import com.snowy.hidden.biz.domain.resp.*;
import com.snowy.hidden.biz.enums.AssignmentStatusEnum;
import com.snowy.hidden.biz.enums.AuditResultEnum;
import com.snowy.hidden.biz.enums.TaskApprovalEnum;
import com.snowy.hidden.biz.enums.TaskCurrentStatusEnum;
import com.snowy.hidden.biz.mapper.HiddenReportMapper;
import com.snowy.hidden.biz.mapper.TaskAssignmentMapper;
import com.snowy.hidden.biz.mapper.TaskLogMapper;
import com.snowy.hidden.biz.mapper.TaskMapper;
import com.snowy.hidden.biz.service.DictService;
import com.snowy.hidden.biz.service.TaskAuditRecordService;
import com.snowy.hidden.biz.service.TaskService;
import com.snowy.hidden.constant.AuthConstant;
import com.snowy.hidden.user.domain.model.AppletUser;
import com.snowy.hidden.user.domain.model.SysUser;
import com.snowy.hidden.user.domain.req.UserListByRoleListReq;
import com.snowy.hidden.user.mapper.AppletUserMapper;
import com.snowy.hidden.user.mapper.SysUserMapper;
import com.snowy.hidden.user.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 任务表(Task)表服务实现类
 *
 * @author cl
 * @since 2025-11-13 13:56:06
 */
@Service("taskService")
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task> implements TaskService {

    @Resource
    private HiddenReportMapper hiddenReportMapper;
    @Resource
    private TaskMapper taskMapper;
    @Resource
    private SysUserMapper sysUserMapper;
    @Resource
    private DictService dictService;
    @Resource
    private SysRoleService sysRoleService;
    @Resource
    private TaskAuditRecordService taskAuditRecordService;
    @Resource
    private TaskAssignmentMapper taskAssignmentMapper;
    @Autowired
    private AppletUserMapper appletUserMapper;
    @Resource
    private TaskLogMapper taskLogMapper;

    @Override
    public BaseResp addTask(TaskAddReq taskAddReq, LoginUser userInfo) {
        //1、添加任务
        Task task = new Task();
        task.setTaskName(taskAddReq.getTaskName());
        task.setRequirements(taskAddReq.getRequirements());
        task.setRewards(taskAddReq.getRewards()*100);
        task.setCreateTime(DateUtil.date());
        //2、反查隐患，获取隐患信息填入任务表信息
        HiddenReport hiddenReport = hiddenReportMapper.selectById(taskAddReq.getReportId());
        if (hiddenReport == null){
            throw new UABPException("task","隐患查询出错，没有当前隐患！");
        }
        task.setReportId(taskAddReq.getReportId());
        task.setReportTime(hiddenReport.getCreateTime());
        task.setApproval(TaskApprovalEnum.WAIT_CHARGE_AUDIT.getValue());
        task.setAddress(hiddenReport.getAddress());
        task.setProvince(hiddenReport.getProvince());
        task.setCity(hiddenReport.getCity());
        task.setDistrict(hiddenReport.getDistrict());
        task.setLatitude(hiddenReport.getLatitude());
        task.setLongitude(hiddenReport.getLongitude());
        task.setHiddenLevel(hiddenReport.getHiddenLevel());
        //3、反向查询分管领导
        SysUser sysUser = sysUserMapper.selectById(hiddenReport.getChargeLeaderId());
        if (sysUser == null){
            throw new UABPException("task","查询分管领导失败！");
        }
        task.setChargeLeaderId(sysUser.getId());
        task.setChargeLeaderName(sysUser.getName());
        this.save(task);

        // 写入审批记录
        TaskAuditRecord taskAuditRecord = new TaskAuditRecord();
        taskAuditRecord.setTaskId(task.getTaskId());
        taskAuditRecord.setTaskName("审核管理员发起流程");
        taskAuditRecord.setCreateUserId(userInfo.getPlatformUserId());
        taskAuditRecord.setCreateUserName(userInfo.getUserName());
        taskAuditRecord.setNextUserId(sysUser.getId());
        taskAuditRecord.setNextUserName(sysUser.getName());
        //当前状态
        taskAuditRecord.setCurrentStatus(TaskApprovalEnum.WAIT_ALLOCATE.getValue());
        //下一个状态
        taskAuditRecord.setNextStatus(TaskApprovalEnum.WAIT_CHARGE_AUDIT.getValue());
        taskAuditRecordService.save(taskAuditRecord);
        return BaseRespUtil.Success();
    }

    @Override
    public BaseResp<List<TaskListResp>> queryTaskList(TaskListReq taskListReq) {
        //添加查询条件
        LambdaQueryWrapper<Task> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //reportId不为空
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getReportId()),Task::getReportId, taskListReq.getReportId());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getCurrentStatus()),Task::getCurrentStatus, taskListReq.getCurrentStatus());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getTaskName()),Task::getTaskName, taskListReq.getTaskName());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getChargeLeaderId()),Task::getChargeLeaderId, taskListReq.getChargeLeaderId());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getApproval()),Task::getApproval, taskListReq.getApproval());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getPayed()),Task::getPayed, taskListReq.getPayed());
        lambdaQueryWrapper.orderByAsc(Task::getCreateTime);


        //分页查询
        List<Task> taskList = this.getBaseMapper().selectList(lambdaQueryWrapper);
        List<TaskListResp> hiddenReportRespList = taskList.stream().map(hiddenReport -> {
            TaskListResp taskListResp = Convert.convert(TaskListResp.class, hiddenReport);
            taskListResp.setApprovalStr(TaskApprovalEnum.getNameByValue(hiddenReport.getApproval()));
            //如果当前状态不为空则返回状态名称
            if (ObjectUtil.isNotEmpty(hiddenReport.getCurrentStatus())){
                taskListResp.setCurrentStatusStr(TaskCurrentStatusEnum.getNameByValue(hiddenReport.getCurrentStatus()));
            }
            //回填隐患等级，如果隐患等级不为空
            taskListResp.setHiddenLevelStr(ObjectUtil.isNull(hiddenReport.getHiddenLevel()) ? "未知" : dictService.getChildeLableByValue("hiddenLevel", hiddenReport.getHiddenLevel().toString()));
            return taskListResp;
        }).collect(Collectors.toList());
        return BaseRespUtil.Success(hiddenReportRespList);
    }

    @Override
    public BaseResp<List<TaskListResp>> queryApprovalTaskList(TaskListReq taskListReq) {
        LoginUser userInfo = AuthConstant.userLocal.get();
        //添加查询条件
        LambdaQueryWrapper<Task> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //reportId不为空
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getReportId()),Task::getReportId, taskListReq.getReportId());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getCurrentStatus()),Task::getCurrentStatus, taskListReq.getCurrentStatus());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getTaskName()),Task::getTaskName, taskListReq.getTaskName());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getChargeLeaderId()),Task::getChargeLeaderId, taskListReq.getChargeLeaderId());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getApproval()),Task::getApproval, taskListReq.getApproval());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getPayed()),Task::getPayed, taskListReq.getPayed());
        //获取审批人的任务
        lambdaQueryWrapper.eq(Task::getChargeLeaderId, userInfo.getPlatformUserId());
        lambdaQueryWrapper.orderByAsc(Task::getCreateTime);


        //分页查询
        List<Task> taskList = this.getBaseMapper().selectList(lambdaQueryWrapper);
        List<TaskListResp> hiddenReportRespList = taskList.stream().map(hiddenReport -> {
            TaskListResp taskListResp = Convert.convert(TaskListResp.class, hiddenReport);
            taskListResp.setApprovalStr(TaskApprovalEnum.getNameByValue(hiddenReport.getApproval()));
            //如果当前状态不为空则返回状态名称
            if (ObjectUtil.isNotEmpty(hiddenReport.getCurrentStatus())){
                taskListResp.setCurrentStatusStr(TaskCurrentStatusEnum.getNameByValue(hiddenReport.getCurrentStatus()));
            }
            //回填隐患等级，如果隐患等级不为空
            taskListResp.setHiddenLevelStr(ObjectUtil.isNull(hiddenReport.getHiddenLevel()) ? "未知" : dictService.getChildeLableByValue("hiddenLevel", hiddenReport.getHiddenLevel().toString()));
            return taskListResp;
        }).collect(Collectors.toList());
        return BaseRespUtil.Success(hiddenReportRespList);
    }

    @Override
    public BaseResp<TaskDetailResp> queryTaskDetail(TaskDetailReq taskDetailReq) {
        if (ObjectUtil.isEmpty(taskDetailReq.getTaskId())){
            throw new UABPException("task","任务id不能为空！");
        }
        //查询任务详情
        Task task = this.getBaseMapper().selectById(taskDetailReq.getTaskId());
        TaskDetailResp taskDetailResp = Convert.convert(TaskDetailResp.class, task);
        //转换当前状态
        taskDetailResp.setCurrentStatusStr(TaskCurrentStatusEnum.getNameByValue(task.getCurrentStatus()));
        //转换隐患等级
        taskDetailResp.setHiddenLevelStr(ObjectUtil.isNull(task.getHiddenLevel()) ? "未知" : dictService.getChildeLableByValue("hiddenLevel", task.getHiddenLevel().toString()));

        //获取上报详情
        if (ObjectUtil.isEmpty(task.getReportId())){
            throw new UABPException("task","上报id为空！");
        }
        HiddenReport hiddenReport = hiddenReportMapper.selectById(task.getReportId());
        HiddenReportResp hiddenReportResp = Convert.convert(HiddenReportResp.class, hiddenReport);
        //类型转换
        hiddenReportResp.setHiddenTypeStr(dictService.getChildeLableByValue("hiddenType", hiddenReport.getHiddenType().toString()));
        //风险等级 如果 hiddenReport.getHiddenLevel()为null则返回未知
        hiddenReportResp.setHiddenLevelStr(ObjectUtil.isNull(hiddenReport.getHiddenLevel()) ? "未知" : dictService.getChildeLableByValue("hiddenLevel", hiddenReport.getHiddenLevel().toString()));
        taskDetailResp.setHiddenReport(hiddenReportResp);
        return BaseRespUtil.Success(taskDetailResp);
    }

    @Override
    @Transactional
    public BaseResp chargeAudit(TaskAuditReq taskAuditReq) {
        //获取登陆人
        LoginUser userInfo = AuthConstant.userLocal.get();
        //1、修改状态查询任务
        Task task = this.getBaseMapper().selectById(taskAuditReq.getTaskId());
        if (ObjectUtil.isEmpty(task)){
            throw new UABPException("task","任务不存在！");
        }
        task.setRewards(taskAuditReq.getRewards());
        task.setRequirements(taskAuditReq.getRequirements());
        task.setTaskName(taskAuditReq.getTaskName());

        TaskAuditRecord taskAuditRecord = new TaskAuditRecord();
        taskAuditRecord.setTaskId(taskAuditReq.getTaskId());
        taskAuditRecord.setTaskName("分管领导审批");
        taskAuditRecord.setCreateUserId(userInfo.getPlatformUserId());
        //获取平台用户
        SysUser sysUser = sysUserMapper.selectById(userInfo.getPlatformUserId());
        if (ObjectUtil.isNotEmpty(sysUser)) {
            taskAuditRecord.setCreateUserName(sysUser.getName());
        }
        taskAuditRecord.setCreateTime(DateUtil.date());
        //审批判定
        if(ObjectUtil.notEqual(AuditResultEnum.PASS.getValue(), taskAuditReq.getAuditResult())){
            //不通过
            task.setApproval(TaskApprovalEnum.WAIT_ALLOCATE.getValue());
            //写入审批记录
            taskAuditRecord.setAuditStatus(AuditResultEnum.NOT_PASS.getValue());
            //当前状态
            taskAuditRecord.setCurrentStatus(task.getApproval());
            //下一个状态
            taskAuditRecord.setNextStatus(TaskApprovalEnum.WAIT_ALLOCATE.getValue());
        }else {
            //不通过
            task.setApproval(TaskApprovalEnum.FINISHED.getValue());
            task.setCurrentStatus(TaskCurrentStatusEnum.RELEASED.getValue());
            task.setRewards(taskAuditReq.getRewards()*100);
            task.setRequirements(taskAuditReq.getRequirements());
            //写入审批记录
            taskAuditRecord.setAuditStatus(AuditResultEnum.PASS.getValue());
            //当前状态
            taskAuditRecord.setCurrentStatus(task.getApproval());
            //下一个状态
            taskAuditRecord.setNextStatus(TaskApprovalEnum.FINISHED.getValue());
            //下一个流程是审核管理员
            // 取审核管理员信息
            UserListByRoleListReq userListByRoleListReq= new UserListByRoleListReq();
            userListByRoleListReq.setRoleCodeList(Arrays.asList("role_audit"));
            List<SysUser> userList = sysRoleService.getUserListByRoleCodeList(userListByRoleListReq);
            if(ObjectUtil.isEmpty(userList)){
                throw new UABPException("task","未配置审核管理员！");
            }
            taskAuditRecord.setNextUserId(userList.get(0).getId());
            taskAuditRecord.setNextUserName(userList.get(0).getName());
        }

        taskAuditRecord.setRemark(taskAuditReq.getAuditRemark());

        this.getBaseMapper().updateById(task);
        taskAuditRecordService.save(taskAuditRecord);
        return BaseRespUtil.Success();
    }

    @Override
    public BaseResp taskAssignment(TaskAuditReq taskAuditReq) {
        //获取登陆人
        LoginUser userInfo = AuthConstant.userLocal.get();
        if (ObjectUtil.isEmpty(taskAuditReq.getTaskId())){
            throw new UABPException("task","任务id不能为空！");
        }
        Task task = this.getBaseMapper().selectById(taskAuditReq.getTaskId());
        if (ObjectUtil.isEmpty(task)){
            throw new UABPException("task","任务不存在！");
        }
        //不能重复领取
        LambdaQueryWrapper<TaskAssignment> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(TaskAssignment::getTaskId, taskAuditReq.getTaskId());
        queryWrapper.eq(TaskAssignment::getAppletUserId, userInfo.getUserId());
        if (taskAssignmentMapper.selectCount(queryWrapper)>0){
            throw new UABPException("task","任务已领取！");
        }
        //保存任务申请记录
        TaskAssignment taskAssignment = new TaskAssignment();
        taskAssignment.setTaskId(task.getTaskId());
        taskAssignment.setTaskName(task.getTaskName());
        taskAssignment.setRequirements(task.getRequirements());
        taskAssignment.setRewards(task.getRewards());
        taskAssignment.setApplyTime(DateUtil.date());
        taskAssignment.setAssignmentStatus(AssignmentStatusEnum.WAIT_ASSIGN.getValue());
        taskAssignment.setAppletUserId(userInfo.getUserId());//小程序id
        //查询平台用户
        SysUser sysUser = sysUserMapper.selectById(userInfo.getPlatformUserId());
        if (ObjectUtil.isNotEmpty(sysUser)) {
            taskAssignment.setAppletPlatformUserId(sysUser.getId());
            taskAssignment.setAppletPlatformUserName(sysUser.getName());
        }
        taskAssignmentMapper.insert(taskAssignment);
        return BaseRespUtil.Success();
    }

    @Override
    public BaseResp<List<TaskListResp>> queryTaskListByApplet(TaskListReq taskListReq) {
        //获取登陆人
        LoginUser userInfo = AuthConstant.userLocal.get();
        //添加查询条件
        LambdaQueryWrapper<Task> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //reportId不为空
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getReportId()),Task::getReportId, taskListReq.getReportId());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getCurrentStatus()),Task::getCurrentStatus, taskListReq.getCurrentStatus());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getTaskName()),Task::getTaskName, taskListReq.getTaskName());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getChargeLeaderId()),Task::getChargeLeaderId, taskListReq.getChargeLeaderId());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getApproval()),Task::getApproval, taskListReq.getApproval());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getPayed()),Task::getPayed, taskListReq.getPayed());
        lambdaQueryWrapper.orderByAsc(Task::getCreateTime);


        //分页查询
        List<Task> taskList = this.getBaseMapper().selectList(lambdaQueryWrapper);
        List<TaskListResp> hiddenReportRespList = taskList.stream().filter(taskTemp -> {
            //通过当前登录人id和任务id，查询任务申请单，如果不存在则返回true
            return taskAssignmentMapper.selectOne(new LambdaQueryWrapper<TaskAssignment>().eq(TaskAssignment::getTaskId, taskTemp.getTaskId()).eq(TaskAssignment::getAppletUserId, userInfo.getUserId())) == null;
            }).map(hiddenReport -> {
            TaskListResp taskListResp = Convert.convert(TaskListResp.class, hiddenReport);
            taskListResp.setApprovalStr(TaskApprovalEnum.getNameByValue(hiddenReport.getApproval()));
            //如果当前状态不为空则返回状态名称
            if (ObjectUtil.isNotEmpty(hiddenReport.getCurrentStatus())){
                taskListResp.setCurrentStatusStr(TaskCurrentStatusEnum.getNameByValue(hiddenReport.getCurrentStatus()));
            }
            //回填隐患等级，如果隐患等级不为空
            taskListResp.setHiddenLevelStr(ObjectUtil.isNull(hiddenReport.getHiddenLevel()) ? "未知" : dictService.getChildeLableByValue("hiddenLevel", hiddenReport.getHiddenLevel().toString()));
            return taskListResp;
        }).collect(Collectors.toList());
        return BaseRespUtil.Success(hiddenReportRespList);
    }

    @Override
    public BaseResp<List<TaskListAssigmentResp>> queryTaskListByApply(TaskListReq taskListReq) {
        //添加查询条件
        LambdaQueryWrapper<Task> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //reportId不为空
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getReportId()),Task::getReportId, taskListReq.getReportId());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getTaskName()),Task::getTaskName, taskListReq.getTaskName());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getChargeLeaderId()),Task::getChargeLeaderId, taskListReq.getChargeLeaderId());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getApproval()),Task::getApproval, taskListReq.getApproval());
        //获取未被领取的任务
        lambdaQueryWrapper.isNull(Task::getReceiverTime);
        lambdaQueryWrapper.isNull(Task::getReceiverAppletUserId);
        lambdaQueryWrapper.eq(Task::getPayed, 0);
        lambdaQueryWrapper.orderByAsc(Task::getCreateTime);


        //分页查询
        List<Task> taskList = this.getBaseMapper().selectList(lambdaQueryWrapper);
        List<TaskListAssigmentResp> taskListAssigmentRespList = taskList.stream().filter(taskTemp -> {
            //通过查询任务申请单，如果不存在则返回true
            return taskAssignmentMapper.selectCount(new LambdaQueryWrapper<TaskAssignment>().eq(TaskAssignment::getTaskId, taskTemp.getTaskId()))>0;
        }).map(hiddenReport -> {
            TaskListAssigmentResp taskListAssigmentResp=new TaskListAssigmentResp();
            //填充任务
            TaskDetailResp taskDetailResp = Convert.convert(TaskDetailResp.class, hiddenReport);
            taskDetailResp.setApprovalStr(TaskApprovalEnum.getNameByValue(hiddenReport.getApproval()));
            //如果当前状态不为空则返回状态名称
            if (ObjectUtil.isNotEmpty(hiddenReport.getCurrentStatus())){
                taskDetailResp.setCurrentStatusStr(TaskCurrentStatusEnum.getNameByValue(hiddenReport.getCurrentStatus()));
            }
            //回填隐患等级，如果隐患等级不为空
            taskDetailResp.setHiddenLevelStr(ObjectUtil.isNull(hiddenReport.getHiddenLevel()) ? "未知" : dictService.getChildeLableByValue("hiddenLevel", hiddenReport.getHiddenLevel().toString()));
            //查询隐患详情
            HiddenReport hiddenReportTemp =hiddenReportMapper.selectById(taskDetailResp.getReportId());
            HiddenReportResp hiddenReportResp = Convert.convert(HiddenReportResp.class, hiddenReportTemp);
            //获取隐患类型
            hiddenReportResp.setHiddenTypeStr(ObjectUtil.isNull(hiddenReportResp.getHiddenType()) ? "未知" : dictService.getChildeLableByValue("hiddenType", hiddenReportResp.getHiddenType().toString()));
            taskDetailResp.setHiddenReport(hiddenReportResp);
            taskListAssigmentResp.setTask(taskDetailResp);
            //填充报名人员及报名人员信息
            List<TaskAssigmentUser> taskAssigmentUsers=new ArrayList<>();
            //1、通过任务id查询任务申请单，获取人员id
            List<TaskAssignment> taskAssignmentList = taskAssignmentMapper.selectList(new LambdaQueryWrapper<TaskAssignment>().eq(TaskAssignment::getTaskId, hiddenReport.getTaskId()));
            taskAssignmentList.forEach(taskAssignment -> {
                //2、通过小程序人员id查询人员信息
                TaskAssigmentUser taskAssigmentUser =new TaskAssigmentUser() ;
                //获取人员小程序信息
                AppletUser appletUser=appletUserMapper.selectOne(new LambdaQueryWrapper<AppletUser>().eq(AppletUser::getUserId, taskAssignment.getAppletUserId()));
                taskAssigmentUser.setAppletUserId(appletUser.getUserId());
                taskAssigmentUser.setAppletUserName(appletUser.getNickName());
                //注册时间
                taskAssigmentUser.setRegisterTime(appletUser.getCreateTime());
                //获取人员平台信息
                SysUser sysUser = sysUserMapper.selectById(appletUser.getPlatformUserId());
                if (ObjectUtil.isNotEmpty(sysUser)){
                    taskAssigmentUser.setPlatformUserId(sysUser.getId());
                    taskAssigmentUser.setPlatformUserName(sysUser.getName());
                }
                //统计当前人员申请
                taskAssigmentUser.setApplyTaskNum(taskAssignmentMapper.selectCount(new LambdaQueryWrapper<TaskAssignment>().eq(TaskAssignment::getAppletUserId, taskAssignment.getAppletUserId())));
                List<Task> taskListTemp = taskMapper.selectList(new LambdaQueryWrapper<Task>().eq(Task::getReceiverAppletUserId, taskAssignment.getAppletUserId()).eq(Task::getCurrentStatus, TaskCurrentStatusEnum.FINISHED.getValue()));
                //统计当前人员完成任务数量
                taskAssigmentUser.setCompleteTaskNum(taskListTemp.size());
                //统计当前人员完成金额
                taskAssigmentUser.setCompleteRewardNum(taskListTemp.stream().mapToInt(Task::getRewards).sum()/100);
                taskAssigmentUsers.add(taskAssigmentUser);
            });
            taskListAssigmentResp.setTaskAssigmentUsers(taskAssigmentUsers);
            return taskListAssigmentResp;
        }).collect(Collectors.toList());
        return BaseRespUtil.Success(taskListAssigmentRespList);
    }

    @Transactional
    @Override
    public BaseResp assignmentUser(AssigmentUserReq assigmentUserReq) {
        //获取登陆人
        LoginUser userInfo = AuthConstant.userLocal.get();
        //1、通过任务id和申请人员id 查询申请单
        TaskAssignment taskAssignment = taskAssignmentMapper.selectOne(new LambdaQueryWrapper<TaskAssignment>().eq(TaskAssignment::getTaskId, assigmentUserReq.getTaskId()).eq(TaskAssignment::getAppletUserId, assigmentUserReq.getAppletUserId()));
        if (ObjectUtil.isNull(taskAssignment)){
            throw new UABPException("task","申请单不存在！");
        }
        taskAssignment.setAssignmentStatus(AssignmentStatusEnum.ASSIGNED.getValue());
        taskAssignment.setAssignmentAppletId(userInfo.getUserId());
        taskAssignment.setAssignmentId(userInfo.getPlatformUserId());
        //通过小程序用户id查询系统用户信息
        AppletUser appletUser = appletUserMapper.selectById(taskAssignment.getAppletUserId());
        if (ObjectUtil.isNotEmpty(appletUser.getPlatformUserId())){
            SysUser sysUser = sysUserMapper.selectById(appletUser.getPlatformUserId());
            taskAssignment.setAppletPlatformUserId(sysUser.getId());
            taskAssignment.setAppletPlatformUserName(sysUser.getName());
        }
        taskAssignment.setAssignmentTime(DateUtil.date());
        //修改任务
        Task task = taskMapper.selectById(taskAssignment.getTaskId());
        task.setCurrentStatus(TaskCurrentStatusEnum.EXECUTING.getValue());
        task.setReceiverAppletUserId(taskAssignment.getAppletUserId());
        task.setReceiverTime(DateUtil.date());
        taskMapper.updateById(task);
        taskAssignmentMapper.updateById(taskAssignment);

        return BaseRespUtil.Success();
    }

    @Override
    public BaseResp<List<TaskListResp>> myTaskList(TaskListReq taskListReq) {
        //获取登陆人
        LoginUser userInfo = AuthConstant.userLocal.get();
        //添加查询条件
        LambdaQueryWrapper<Task> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        //reportId不为空
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getReportId()),Task::getReportId, taskListReq.getReportId());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getCurrentStatus()),Task::getCurrentStatus, taskListReq.getCurrentStatus());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getTaskName()),Task::getTaskName, taskListReq.getTaskName());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getChargeLeaderId()),Task::getChargeLeaderId, taskListReq.getChargeLeaderId());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getApproval()),Task::getApproval, taskListReq.getApproval());
        lambdaQueryWrapper.eq(ObjectUtil.isNotEmpty(taskListReq.getPayed()),Task::getPayed, taskListReq.getPayed());

        lambdaQueryWrapper.eq(Task::getReceiverAppletUserId, userInfo.getUserId());

        //排序依据当前状态升序
        lambdaQueryWrapper.orderByAsc(Task::getCurrentStatus,Task::getCreateTime);


        //分页查询
        List<Task> taskList = this.getBaseMapper().selectList(lambdaQueryWrapper);
        List<TaskListResp> hiddenReportRespList = taskList.stream().map(hiddenReport -> {
            TaskListResp taskListResp = Convert.convert(TaskListResp.class, hiddenReport);
            taskListResp.setApprovalStr(TaskApprovalEnum.getNameByValue(hiddenReport.getApproval()));
            //如果当前状态不为空则返回状态名称
            if (ObjectUtil.isNotEmpty(hiddenReport.getCurrentStatus())){
                taskListResp.setCurrentStatusStr(TaskCurrentStatusEnum.getNameByValue(hiddenReport.getCurrentStatus()));
            }
            //回填隐患等级，如果隐患等级不为空
            taskListResp.setHiddenLevelStr(ObjectUtil.isNull(hiddenReport.getHiddenLevel()) ? "未知" : dictService.getChildeLableByValue("hiddenLevel", hiddenReport.getHiddenLevel().toString()));
            return taskListResp;
        }).collect(Collectors.toList());
        return BaseRespUtil.Success(hiddenReportRespList);
    }

    @Override
    public BaseResp submitTaskLog(TaskLogSubmitReq taskLogSubmitReq) {
        TaskLog taskLog = new TaskLog();
        //获取登陆人
        LoginUser userInfo = AuthConstant.userLocal.get();
        taskLog.setTaskId(taskLogSubmitReq.getTaskId());
        taskLog.setAppletUserId(userInfo.getUserId());
        //获取用户平台id
        //获取人员平台信息
        SysUser sysUser = sysUserMapper.selectById(userInfo.getPlatformUserId());
        if (ObjectUtil.isNotEmpty(sysUser)){
            taskLog.setPlatformUserId(sysUser.getId());
        }
        taskLog.setReportDate(DateUtil.date());
        taskLog.setCreateTime(DateUtil.date());
        //获取上报图片，组装成逗号分开的字符串
        if (ObjectUtil.isNotEmpty(taskLogSubmitReq.getFliePath())) {
            taskLog.setFliePath(StrUtil.join(",", taskLogSubmitReq.getFliePath()));
        }
        taskLog.setRemark(taskLogSubmitReq.getRemark());
        taskLogMapper.insert(taskLog);
        return BaseRespUtil.Success();
    }

    @Override
    public BaseResp taskLogListById(TaskLogSubmitReq taskLogSubmitReq) {
        //获取登陆人
        LoginUser userInfo = AuthConstant.userLocal.get();
        //通过任务id和小程序用户id查询，查询结果
        List<TaskLog> taskLogList =new ArrayList<>();
        if (ObjectUtil.isEmpty(taskLogSubmitReq.getIsIdentify())){
            taskLogList=taskLogMapper.selectList(new LambdaQueryWrapper<TaskLog>().eq(TaskLog::getTaskId, taskLogSubmitReq.getTaskId()).eq(TaskLog::getAppletUserId, userInfo.getUserId()));
        }else {
            taskLogList=taskLogMapper.selectList(new LambdaQueryWrapper<TaskLog>().eq(TaskLog::getTaskId, taskLogSubmitReq.getTaskId()));
        }
        List<TaskLogResp> taskLogRespList=taskLogList.stream().map(x->Convert.convert(TaskLogResp.class, x)).collect(Collectors.toList());
        return BaseRespUtil.Success(taskLogRespList);
    }

    @Override
    public BaseResp taskJudge(TaskJudgeReq taskJudgeReq) {
        //1、获取登陆人
        LoginUser userInfo = AuthConstant.userLocal.get();
        Task task = taskMapper.selectById(taskJudgeReq.getTaskId());
        if (ObjectUtil.isEmpty(task)){
            //抛出异常
            throw new UABPException("task","任务不存在！");
        }
        //修改任务状态
        if (taskJudgeReq.getVerifyResult().equals("1")){
            task.setCurrentStatus(TaskCurrentStatusEnum.FINISHED.getValue());
        }else {
            task.setCurrentStatus(TaskCurrentStatusEnum.FAILED.getValue());

        }
        task.setTaskRemark(taskJudgeReq.getTaskRemark());
        taskMapper.updateById(task);
        return BaseRespUtil.Success();
    }
}
